package com.ysh.smartplatform.controller;

import com.ysh.smartplatform.dto.R;
import com.ysh.smartplatform.pojo.SysDevice;
import com.ysh.smartplatform.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 袁胜虎 on 2022/9/8 20:26
 */
@RestController
@RequestMapping("/devices")
@Api(tags = "设别管理模块")
public class DeviceController {
    private DeviceService deviceService;

    @Autowired
    public void setDeviceService(DeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @PostMapping("/device")
    @ApiOperation("添加设备")
    public R addDevice(@RequestBody @Validated SysDevice sysDevice) {
        deviceService.addDevice(sysDevice);
        return R.OK();
    }

//    @GetMapping("/device/{id}")
//    public R findDeviceById(@Validated String id){
//        deviceService.findByDeviceId(id);
//        return R.OK();
//    }

    @DeleteMapping("/device/{id}")
    @ApiOperation("根据id删除设备")
    public R deleteDeviceById(@Validated @PathVariable String id){
        deviceService.deleteDeviceById(id);
        return  R.OK();
    }

    @PostMapping("/device/update")
    @ApiOperation("更新设备")
    public R updateDeviceById(@RequestBody @Validated SysDevice sysDevice){
        deviceService.updateDevice(sysDevice);
        return  R.OK();
    }
    @GetMapping("/device/{id}")
    @ApiOperation("根据id查找设备")
    public R findById(@PathVariable  String id) {
        return R.OK(deviceService.findById(id));
    }
    @GetMapping("")
    public R findAllCategory(){
        return  R.OK(deviceService.findAllDevice());
    }

    @PostMapping("/command/{deviceId}/{command}")
    @ApiOperation("获取命令")
    public R sendCommand(@PathVariable("deviceId") String deviceId,@PathVariable("command") String command){
        deviceService.sendCommand(deviceId,command);
        return R.OK();
    }

}
